/**
 * Copyright 2004 - 2007 Blue Bamboo International Inc. 
 *           All rights reserved.
 *
 *
 * Blue Bamboo PROPRIETARY/CONFIDENTIAL.
 *
 */

package com.bluebamboo.bluetools.pingpang.ui;

import java.io.IOException;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.jface.preference.PreferenceManager;
import org.eclipse.jface.preference.PreferenceStore;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabFolder2Listener;
import org.eclipse.swt.custom.CTabFolderEvent;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import com.bluebamboo.bluetools.common.ui.ExceptionDialog;
import com.bluebamboo.bluetools.common.ui.utils.SWTFactory;
import com.bluebamboo.bluetools.pingpang.ConnectionManager;
import com.bluebamboo.bluetools.pingpang.bean.ConnectionBean;

/**
 * DOCME
 * 
 * @author Nick.Tan
 * 
 */
public class PingPangWindow extends ApplicationWindow {

	private CTabFolder tabFolder;
	private ConnectAction connAction;
	private ConnectionManager connMgr;

	/**
	 * @param parentShell
	 */
	public PingPangWindow(Shell parentShell) {
		super(parentShell);
		connMgr = ConnectionManager.getInstance();
		addMenuBar();
		addStatusLine();
		addToolBar(SWT.FLAT);
	}

	@Override
	protected Control createContents(Composite parent) {
		Shell shell = getShell();
		shell.setText("PingPang-v1.0");
		shell.setSize(500, 600);

		Composite mainContainer = SWTFactory.createComposite(parent, parent.getFont(), 1, 1, GridData.FILL_BOTH);
		Label lineSep = new Label(mainContainer, SWT.SEPARATOR | SWT.HORIZONTAL);
		GridDataFactory.fillDefaults().grab(true, false).applyTo(lineSep);

		tabFolder = new CTabFolder(mainContainer, SWT.TOP);
		GridDataFactory.fillDefaults().grab(true, true).applyTo(tabFolder);
		tabFolder.addCTabFolder2Listener(new CTabFolder2Listener() {
			public void showList(CTabFolderEvent event) {
			}

			public void restore(CTabFolderEvent event) {
			}

			public void minimize(CTabFolderEvent event) {
			}

			public void maximize(CTabFolderEvent event) {
			}

			public void close(CTabFolderEvent event) {
				ConnectionBean connBean = (ConnectionBean) ((CTabItem) event.item).getData();
				try {
					if (connBean != null)
						connMgr.close(connBean.getName());
				}
				catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		CTabItem welcomeTab = new CTabItem(tabFolder, SWT.NONE);
		welcomeTab.setText("Welcome");
		Composite welComp = SWTFactory.createComposite(tabFolder, 1, GridData.FILL_BOTH);
		welcomeTab.setControl(welComp);

		return mainContainer;
	}

	@Override
	protected MenuManager createMenuManager() {
		MenuManager menuManager = super.createMenuManager();

		MenuManager fileMenuManager = new MenuManager("&File");
		fileMenuManager.add(getConnectAction());
		MenuManager prefMenuManager = new MenuManager("&Preferences");
		prefMenuManager.add(new OptionAction());

		menuManager.add(fileMenuManager);
		menuManager.add(prefMenuManager);

		return menuManager;
	}

	@Override
	protected ToolBarManager createToolBarManager(int style) {
		ToolBarManager barManager = super.createToolBarManager(style);
		barManager.add(getConnectAction());
		return barManager;
	}

	@Override
	protected void handleFontChange(PropertyChangeEvent event) {
		try {
			connMgr.closeAll();
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private ConnectAction getConnectAction() {
		if (connAction == null)
			connAction = new ConnectAction();
		return connAction;
	}

	private class ConnectAction extends Action {

		ConnectAction() {
			super("Connect");
		}

		@Override
		public void run() {
			ConnectDialog dialog = new ConnectDialog(getShell());
			if (dialog.open() == Window.OK) {
				ConnectionBean connBean = dialog.getConnectionBean();
				CTabItem item = new CTabItem(tabFolder, SWT.NONE);
				PingPangPanel panel = new PingPangPanel();
				item.setControl(panel.createContents(tabFolder));
				item.setShowClose(true);
				item.setText(connBean.getName());
				item.setData(connBean);
				try {
					connMgr.open(connBean, panel);
				}
				catch (Exception e) {
					ExceptionDialog.openError(getShell(), "Error", "connect failure", e);
					return;
				}
				tabFolder.setSelection(item);
			}
		}
	}

	private class OptionAction extends Action {

		OptionAction() {
			super("Options");
		}

		@Override
		public void run() {
			PreferenceManager prefMgr = new PreferenceManager();
			PreferenceDialog dialog = new PreferenceDialog(null, prefMgr);
			PreferenceStore ps = new PreferenceStore("preference.properties");
			try {
				ps.load();
			}
			catch (IOException e) {
				// Ignore
			}
			dialog.setPreferenceStore(ps);
			dialog.open();

			try {
				// Save the preferences
				ps.save();
			}
			catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
